#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/8 17:06
# @Author  : jeff wu
# @FileName: scheduler_config
from apscheduler.schedulers.background import BackgroundScheduler
import threading
from flask import current_app
from src.utils.scheduler_job import SchedulerJob
from src.utils.read_config import server_address_config

remote_url, is_proactive_request,file_path = server_address_config()

IS_PROACTIVE_REQUEST  = is_proactive_request


class Scheduler:
    """
    Singleton pattern
    """
    _instance_lock = threading.Lock()
    __instance = None

    @staticmethod
    def init_apscheduler(app=None):
        """
        Initialize the scheduled task scheduler
        :return:
        """
        if app is None:
            app = current_app._get_current_object()

        with Scheduler._instance_lock:
            if Scheduler.__instance is None:
                Scheduler.__instance = BackgroundScheduler()
                Scheduler.__instance.start()
            if Scheduler.__instance.get_job('my_job'):
                Scheduler.__instance.remove_job('my_job')
            scheduler_job_instance = SchedulerJob()
            Scheduler.__instance.add_job(func=scheduler_job_instance.process_tasks, args=[app], trigger='interval', seconds=20)
            Scheduler.__instance.add_job(func=scheduler_job_instance.get_website_info_send, args=[app], trigger='interval', hours=1)
            if IS_PROACTIVE_REQUEST:
                Scheduler.__instance.add_job(func=scheduler_job_instance.get_ip_info, args=[app], trigger='interval', days=1)
                Scheduler.__instance.add_job(func=scheduler_job_instance.get_website_info, args=[app], trigger='interval', days=1)
        return Scheduler.__instance


# scheduler = Scheduler().init_apscheduler()
